package com.ruoyi.controller.quality;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.quality.MesQualityItem;
import com.ruoyi.service.quality.IMesQualityItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 质量检验项目Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/quality/item")
public class MesQualityItemController extends BaseController
{
    @Autowired
    private IMesQualityItemService IMesQualityItemService;

    /**
     * 查询质量检验项目列表
     */
    @PreAuthorize("@ss.hasPermi('quality:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesQualityItem mesQualityItem)
    {
        startPage();
        List<MesQualityItem> list = IMesQualityItemService.selectMesQualityItemList(mesQualityItem);
        return getDataTable(list);
    }

    /**
     * 导出质量检验项目列表
     */
    @PreAuthorize("@ss.hasPermi('quality:item:export')")
    @Log(title = "质量检验项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesQualityItem mesQualityItem)
    {
        List<MesQualityItem> list = IMesQualityItemService.selectMesQualityItemList(mesQualityItem);
        ExcelUtil<MesQualityItem> util = new ExcelUtil<MesQualityItem>(MesQualityItem.class);
        util.exportExcel(response, list, "质量检验项目数据");
    }

    /**
     * 获取质量检验项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('quality:item:query')")
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId)
    {
        return success(IMesQualityItemService.selectMesQualityItemByItemId(itemId));
    }

    /**
     * 新增质量检验项目
     */
    @PreAuthorize("@ss.hasPermi('quality:item:add')")
    @Log(title = "质量检验项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesQualityItem mesQualityItem)
    {
//        if ("1".equals(IMesQualityItemService.checkItemCodeUnique(mesQualityItem.getStandardCode(),mesQualityItem.getStandardId())))
//        {
//            return error("新增检验标准'" + mesQualityItem.getStandardCode() + "'失败，检验标准编码已存在");
//        }
        return toAjax(IMesQualityItemService.insertMesQualityItem(mesQualityItem));
    }

    /**
     * 修改质量检验项目
     */
    @PreAuthorize("@ss.hasPermi('quality:item:edit')")
    @Log(title = "质量检验项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesQualityItem mesQualityItem)
    {
        if ("1".equals(IMesQualityItemService.checkItemCodeUnique(mesQualityItem.getStandardCode(),mesQualityItem.getStandardId())))
        {
            return error("修改检验标准'" + mesQualityItem.getStandardCode() + "'失败，检验标准编码已存在");
        }
        return toAjax(IMesQualityItemService.updateMesQualityItem(mesQualityItem));
    }

    /**
     * 删除质量检验项目
     */
    @PreAuthorize("@ss.hasPermi('quality:item:remove')")
    @Log(title = "质量检验项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds)
    {
        return toAjax(IMesQualityItemService.deleteMesQualityItemByItemIds(itemIds));
    }

    /**
     * 校验检验项目编码是否唯一
     */
    @GetMapping("/checkItemCodeUnique")
    public AjaxResult checkItemCodeUnique(String itemCode , Long itemId)
    {
        String result = IMesQualityItemService.checkItemCodeUnique(itemCode,itemId);
        return AjaxResult.success(result.equals("1")); // 返回true表示编码已存在，false表示编码可用
    }

    /**
     * 根据标准ID查询检验项目列表
     */
    @GetMapping("/standard/{standardId}")
    public AjaxResult getItemsByStandardId(@PathVariable Long standardId)
    {
        List<MesQualityItem> list = IMesQualityItemService.selectItemsByStandardId(standardId);
        return success(list);
    }

    /**
     * 根据标准编码查询检验项目列表
     */
    @GetMapping("/standardCode/{standardCode}")
    public AjaxResult getItemsByStandardCode(@PathVariable String standardCode)
    {
        List<MesQualityItem> list = IMesQualityItemService.selectItemsByStandardCode(standardCode);
        return success(list);
    }

    /**
     * 查询启用状态的检验项目列表
     */
    @GetMapping("/active")
    public AjaxResult getActiveItems()
    {
        List<MesQualityItem> list = IMesQualityItemService.selectActiveItems();
        return success(list);
    }
}